package com.wenda.homeschooedu.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.wenda.homeschooedu.DB.DB;
import com.wenda.homeschooedu.DB.JDBCConnection;
import com.wenda.homeschooedu.dao.Ilesson;
import com.wenda.homeschooedu.model.Lesson;

public class LessonService implements Ilesson {
	private JDBCConnection jdbc;

	private Statement statement;

	public LessonService() {
		this.jdbc = new JDBCConnection();
		this.statement = jdbc.getStm();
	}

	public List<Lesson> getLessonList(int class_id, int year, int term) {
		String sql = String.format(DB.Table.Lesson.SQL.SELECT, class_id, year,
				term);
		List<Lesson> lessons = new ArrayList<Lesson>();
		try {
			ResultSet resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				Lesson lesson = new Lesson();
				lesson.set_id(resultSet.getInt(resultSet.findColumn("_id")));
				lesson.setClassName(resultSet.getString(resultSet
						.findColumn("class_name")));
				lesson.setDay(resultSet.getInt(resultSet.findColumn("day")));
				lesson.setSubjectName(resultSet.getString(resultSet
						.findColumn("subject_name")));
				lesson.setThetime(resultSet.getInt(resultSet
						.findColumn("thetime")));
				lessons.add(lesson);

			}

			return lessons;

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

}
